<?php
/**
 * Created by PhpStorm.
 * User: spring
 * Date: 2022/2/23
 * Time: 16:54
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Google_img extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model(array('google_disease' => 'disease','google_diseaseimg' => 'diseaseimg'));
    }

    public function test(){
//        $url = 'https://www.google.com.hk/imgres?imgurl=https%3A%2F%2Fwww.dovepress.com%2Fcr_data%2Farticle_fulltext%2Fs276000%2F276530%2Fimg%2FVHRM_A_276530_t0002.jpg&imgrefurl=https%3A%2F%2Fwww.dovepress.com%2Fincidence-characteristics-laboratory-findings-and-outcomes-in-acro-isc-peer-reviewed-fulltext-article-VHRM&tbnid=ZpAAkJUUNx3FCM&vet=12ahUKEwjCvKiHiMX2AhUWAzQIHZLoCtIQMygNegQIARA2..i&docid=JC2hsm6wO1rRdM&w=2023&h=2460&q=acro-perceptual%20anomaly%20disease%20disease%20introduction&ved=2ahUKEwjCvKiHiMX2AhUWAzQIHZLoCtIQMygNegQIARA2';
//        $url = urldecode($url);
//        // 切分imgurl
//        preg_match('|imgurl=(.+?)&imgrefurl=|',$url,$urls);
//        var_dump($urls);
        $dir = "C:/Users/Administrator/Downloads/";
        var_dump($files = scandir($dir));
    }

    public function img(){
        $url = 'https://www.google.com.hk/imgres?imgurl=https%3A%2F%2Fraredisorders.imedpub.com%2Farticles-images%2Fraredisorders-Syndactyly-5-3-5-g003.png&imgrefurl=https%3A%2F%2Fraredisorders.imedpub.com%2Fpfraser-syndrome-a-case-report-of-a-very-rare-syndromep.php%3Faid%3D24756&tbnid=BRnH9IQXhZHCaM&vet=12ahUKEwjIuvi7j8X2AhV-CTQIHZMrAPQQMygTegUIARC5AQ..i&docid=PBvYKR4sHMvn1M&w=579&h=348&q=cryptophthalmos-syndactyly%20sydnrome%20disease%20introduction&ved=2ahUKEwjIuvi7j8X2AhV-CTQIHZMrAPQQMygTegUIARC5AQ';
        $url = urldecode($url);
        // 切分imgurl
        preg_match('|imgurl=(.+?)&imgrefurl=|',$url,$urls);
        preg_match('|imgrefurl=(.+?)&docid=|',$url,$refers);
        $base = $urls[1];
        $refer = $refers[1];
        echo $base;

        $ch = curl_init($base);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1');
        curl_setopt($ch, CURLOPT_PROXYPORT, '10809');
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $img = curl_exec($ch);
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($statusCode == 200 && $img){
            echo "succ";
            $fp2=@fopen(FCPATH .'google_pic/test.jpg','a');
            fwrite($fp2,$img);
            fclose($fp2);
        }else{
            echo "no";
        }


//        $this->getImage($base,$save_dir.'/',$img['index'].'.jpg');
//        echo $content;
    }

    public function data($did=0)
    {
//        $ids = json_decode(file_get_contents(FCPATH .'last/old_id.log'),true);
//        $did = $ids[$index];
//        if(!$did){
//            echo "nodata";
//            exit;
//        }
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $did = (int)$redis->lpop('google_pic_ext');
        $disease = $this->db->where('id='.$did)->order_by('id', 'desc')->limit(1)->get('google_disease')->row_array();
        if(!$disease){
            echo "nodata";
            exit;
        }
        // 查看是否存在重名的数据
//        $check_pic = $this->db->where('disease_name',$disease['disease_name_en'])->limit(1)->get('spider_google_disease_pic')->row_array();
//        if($check_pic){
//            $last = $did-1;
//            if($last<5323){
//
//            }else{
//                echo $last;
//            }
//            exit;
//        }
        $did = $disease['id'];
        $imgs = $this->diseaseimg->result_all('where did='.$did);
        if($imgs){

            $old_id = $disease['old_id'];
            $save_dir = FCPATH .'google_pic/'.md5($disease['disease_name_en']).rand(10,99);
            // 判断是否存在目录
            if ( ! file_exists($save_dir)) {
                mkdir($save_dir, 0777, true);
            }
            // 在目录下生成一个log文件，写入当前did
            file_put_contents($save_dir."/id.log",$did);
            $img_num = 0;
            $sql = "";
            foreach($imgs as $img){
                    $url = $img['url'];
                    $url = urldecode($url);
                    // 切分imgurl
                    preg_match('|imgurl=(.+?)&imgrefurl=|',$url,$urls);
                    preg_match('|imgrefurl=(.+?)&docid=|',$url,$refers);
                    $base = $urls[1];
                    $refer = $refers[1];
                    if(strpos($base,'x-raw-image') === 0){ // 如果是原生的，直接拿原生的数据
                        $base = $img['base'];
                    }
                    if(strpos($base,'http') === 0){
                        $filename = $save_dir.'\\'.$img['index'].'.jpg';
                        $r = $this->getImage($base,$save_dir.'/',$img['index'].'.jpg',$refer);
                    }else{
                        $ext = preg_match('/\(*)?;/',$base);
                        preg_match('|/(.+?);|', $base, $ext);
                        $ext = $ext[1];
                        if (strstr($base,",")){
                            $base = explode(',',$base);
                            $base = $base[1];
                        }
                        $filename = $save_dir.'/'.$img['index'].".".$ext;
                        $r = file_put_contents($save_dir.'/'.$img['index'].".".$ext, base64_decode($base));//返回的是字节数
                    }
                    // 查看文件是否存在
//                    if($r){
//                        // 修改
//                        $img_up['status'] = 1;
//                        $this->diseaseimg->update($img['id'],$img_up);
//                        // 写入
//                        $url = 'google_pic/'.$img['name'].'/'.$img['index'].'.jpg';
//                        $sql .= '("'.$img['name'].'",'.$old_id.','.$img['index'].',"'.$url.'","'.date("Y-m-d H:i:s").'"),';
//
//                        $img_num++;
//                    }


            }
//            if($img_num > 0){
//                $sql = substr($sql,0,-1);
//                $sql = 'INSERT ignore INTO ai_spider_google_disease_pic (disease_name,disease_id,pic_id,pic_url,last_spider) VALUES '.$sql;
//                //echo $sql;exit;
//                $this->db->query($sql);
//                // 修改本地表状态
//                $up['nums'] = $img_num;
//                $this->disease->update($did,$up);
//
//            }

        }
        echo 1;
//        $last = $did-1;
//        if($last<5323){
//            echo "nodata";
//        }else{
//            echo $last;
//        }

    }

    function bat_img($did){
        if(!$did){
            return false;
        }
        $imgs = $this->diseaseimg->result_all('where status=1 and did='.$did);
        //$dids = array_unique(array_column($imgs,'did'));
        $disease = $this->db->where('id',$did)->get('google_disease')->row_array();
        $sql = "";
        foreach($imgs as $img){
            $old_id = $disease['old_id'];

            $url = 'google_pic/'.$img['name'].'/'.$img['index'].'.jpg';
            $sql .= '("'.$img['name'].'",'.$old_id.','.$img['index'].',"'.$url.'","'.$img['created_at'].'"),';
        }
        $sql = substr($sql,0,-1);
        $sql = 'INSERT ignore INTO ai_spider_google_disease_pic (disease_name,disease_id,pic_id,pic_url,last_spider) VALUES '.$sql;
        //echo $sql;exit;
        $this->db->query($sql);
    }

    function getImage($url,$save_dir='',$filename='',$refer=''){
        if(trim($url)==''){
            return -1;
        }
        if(trim($save_dir)==''){
            $save_dir='./';
        }
        if(trim($filename)==''){//保存文件名
            $ext=strrchr($url,'.');
            if($ext!='.gif'&&$ext!='.jpg'){
                return -2;
            }
            $filename=time().$ext;
        }
        if(0!==strrpos($save_dir,'/')){
            $save_dir.='/';
        }
        //创建保存目录
        if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
            return -3;
        }
        //获取远程文件所采用的方法
        //if($type){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch,CURLOPT_REFERER,'https://www.dovepress.com/incidence-characteristics-laboratory-findings-and-outcomes-in-acro-isc-peer-reviewed-fulltext-article-VHRM&tbnid=ZpAAkJUUNx3FCM&vet=12ahUKEwjCvKiHiMX2AhUWAzQIHZLoCtIQMygNegQIARA2');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);

        curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1');
        curl_setopt($ch, CURLOPT_PROXYPORT, '10809');
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $img = curl_exec($ch);
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($statusCode == 200 && $img){
            $fp2=@fopen($save_dir.$filename,'a');
            fwrite($fp2,$img);
            fclose($fp2);
            unset($img,$url);
            return 1;
        }else{
            return 0;
        }

    }

    /**
     * 移动使用python下载下来的图片
     * 1.先停止content.html 抓取图片
     * 2.等待最后一个目录的数据抓取完毕且写入pic表
     * 3.把目录改成当日日期，并重新新建目录google_pic
     * 4.继续抓取content.html
     * 5.查找最后一条的疾病id（$did），修改python脚本中log_disease的id
     * 6.清空下载目录；运行python脚本，抓取上次没有抓取的数据(imgs 表中 小于$did 且 status=0的数据)
     * 7.等待python运行完成，执行本方法
     * 8.当前方法执行完成以后，把当日的goog_pic目录批量上传到ALIOSS
     */
    public function img_remove($did = 6167){

        $imgs = $this->diseaseimg->result_all('where did='.$did.' and status=0');
        if($imgs){
            $disease = $this->disease->get_by_id($did);
            $old_id = $disease['old_id'];
            $save_dir = FCPATH .'google_pic20220315/'.$imgs[0]['name'];
            // 判断是否存在目录
            if ( ! file_exists($save_dir)) {
                mkdir($save_dir, 0777, true);
            }
            $img_num = 0;
            $sql = "";
            $down_path = "C:/Users/Administrator/Downloads/";
            $down_files = scandir($down_path);
            foreach($imgs as $img){
                $url = $img['url'];
                $url = urldecode($url);
                // 切分imgurl
                preg_match('|imgurl=(.+?)&imgrefurl=|',$url,$urls);
                preg_match('|imgrefurl=(.+?)&docid=|',$url,$refers);
                $base = $urls[1];
                $files = explode("/",$base);
                $file = array_pop($files);
                // 获取文件名，判断下载目录是否存在该文件
                $file = urldecode($file);
                $names = explode(".",$file);
                $name = $names[0];
                if($base = array_search_str($name,$down_files)){ // 如果是原生的，直接拿原生的数据
                    // 移动文件到对应目录下
                    $r = copy($down_path.$base,$save_dir.'/'.$img['index'].".jpg");
                    if($r){
                        unlink($down_path.$base);
                        // 修改
                        $img_up['status'] = 1;
                        $this->diseaseimg->update($img['id'],$img_up);
//                        // 写入
                        $url = 'google_pic/'.$img['name'].'/'.$img['index'].'.jpg';
                        $sql .= '("'.$img['name'].'",'.$old_id.','.$img['index'].',"'.$url.'","'.date("Y-m-d H:i:s").'"),';

                        $img_num++;
                    }
                    //
                }
            }
            if($img_num > 0){
                $sql = substr($sql,0,-1);
                $sql = 'INSERT ignore INTO ai_spider_google_disease_pic (disease_name,disease_id,pic_id,pic_url,last_spider) VALUES '.$sql;
                $this->db->query($sql);
                // 修改本地表状态
                $up['nums'] = $disease['nums']+$img_num; // 累计
                echo $img_num;
                var_dump($up);
                $this->disease->update($did,$up);
                echo $this->db->last_query();
            }

        }
        $last = $did-1;
        if($last<5700){
            echo "nodata";
        }else{
            echo $last;
        }
    }

    public function check(){
//        $save_path = FCPATH .'google_pic/';
//        $dirs = scandir($save_path);
        $dirs =  $this->db->query("select CONCAT(`name`,`index`) as imgs from ai_google_disease_imgs where status=1 and created_at>'2022-03-20'")->result_array();
        $dirs = array_column($dirs,'imgs');
        echo count($dirs);

        //$groups = $this->db->query("select disease_name from ai_spider_google_disease_pic where last_spider>'2022-03-19' group by disease_id")->result_array();
        $groups = $this->db->query("select  CONCAT(`disease_name`,`pic_id`) as pic from ai_spider_google_disease_pic where last_spider>'2022-03-20'")->result_array();
        $groups = array_column($groups,'pic');
//        $res = array_count_values ($groups);
//        asort($res);
//        var_dump($res);
        $res = array_diff($dirs,$groups);
        var_dump($res);
    }

//     public function old_file_data($ind){
//         if(!isset($ind)){
//             echo "nodata";
//             exit;
//         }
//         $save_path = FCPATH .'google_pic20220320/';
//         $dirs = scandir($save_path);
//         $dirs = array_slice($dirs,2);
//         $d = $dirs[$ind];
//         $diseases = $this->db->where('disease_name_en',$d)->get('google_disease')->row_array();
// //        if(!$diseases){
// //            continue;
// //        }
//         $did = $diseases['id'];
//         $files = scandir($save_path.$d);
//         $files = array_slice($files,2);
//         // 使用事务
//         $this->db->trans_begin();
//         $sql = "";
//         //echo "begin:".time()."<br>";
//         foreach($files as $f){
//             $indexs = explode(".",$f);
//             $index = $indexs[0];
//             // 修改imgs
//             $img = $this->db->where(['did'=>$did,'index'=>$index])->get('google_disease_imgs')->row_array();
//             $img_up['status'] = 1;
//             $this->diseaseimg->update($img['id'],$img_up);
//             $url = 'google_pic/'.$d.'/'.$f;
//             $sql .= '("'.$d.'",'.$diseases['old_id'].','.$index.',"'.$url.'","'.date("Y-m-d H:i:s").'"),';
//             //echo "for:".time()."<br>";
//         }
//         $sql = substr($sql,0,-1);
//         $sql = 'INSERT ignore INTO ai_spider_google_disease_pic (disease_name,disease_id,pic_id,pic_url,last_spider) VALUES '.$sql;
//         //echo $sql;exit;
//         $this->db->query($sql);
//         //echo "over:".time()."<br>";

//         $dis_up['nums'] = count($files);
//         $this->disease->update($did,$dis_up);



//         if ($this->db->trans_status() === FALSE){
//             //回滚
//             $this->db->trans_rollback();
//             echo "事务失败".$did;
//             exit;
//         }else{
//             //提交事务
//             $this->db->trans_commit();
//             $last = $ind+1;
//             if($last < count($dirs)){
//                 echo $last;
//             }else{
//                 echo "nodata";
//             }
//         }
//     }

    /**
     * 通过文件来生成数据，保证img和pic的数据问题
     */
    public function file_data($ind){
        // 先获取目录下log文件
        if(!isset($ind)){
            echo "nodata";
            exit;
        }
        $save_path = FCPATH .'google_pic/';
        $dirs = scandir($save_path);
        $dirs = array_slice($dirs,2);
        sort($dirs);
        $d = $dirs[$ind];
        $files = scandir($save_path.$d);
        $files = array_slice($files,2);
        if(!$files){
            $last = $ind+1;
            if($last < count($dirs)){
                echo $last;
            }else{
                echo "nodata";
            }
            exit;
        }
        // 获取did
        $did = file_get_contents($save_path.$d."/id.log");
        if(!$did){
            echo "nodata";
            exit;
        }
        $diseases = $this->disease->get_by_id($did);
//        if(!$diseases){
//            continue;
//        }
        // 使用事务
        $this->db->trans_begin();
        $sql = "";
        //echo "begin:".time()."<br>";
        foreach($files as $f){
            $indexs = explode(".",$f);
            if($indexs[1] == "log") continue;
            $index = $indexs[0];
            // 修改imgs
            $img = $this->db->where(['did'=>$did,'index'=>$index])->get('google_disease_imgs')->row_array();
            $img_up['status'] = 1;
            $this->diseaseimg->update($img['id'],$img_up);
            $url = 'google_pic/'.$d.'/'.$f;
            $sql .= '("'.$d.'",'.$diseases['old_id'].','.$index.',"'.$url.'","'.date("Y-m-d H:i:s").'"),';
            //echo "for:".time()."<br>";
        }
        $sql = substr($sql,0,-1);
        $sql = 'INSERT ignore INTO ai_spider_google_disease_pic (disease_name,disease_id,pic_id,pic_url,last_spider) VALUES '.$sql;
        $this->db->query($sql);
        //echo "over:".time()."<br>";

        $dis_up['nums'] = count($files)-1;
        $this->disease->update($did,$dis_up);



        if ($this->db->trans_status() === FALSE){
            //回滚
            $this->db->trans_rollback();
            echo "nodata".$did;
            exit;
        }else{
            //提交事务
            $this->db->trans_commit();
            $last = $ind+1;
            if($last < count($dirs)){
                echo $last;
            }else{
                echo "nodata";
            }
        }
    }

    public function rename(){
        $save_dir = "C:/Users/Administrator/Desktop/tmp";
        $file = scandir($save_dir);
        dd($file);
    }

    public function ins_redis(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        $diseases = $this->db->where_in("old_id",array(252,534,891,1274,2366,2651,2954,3584,4347,5037,5078,5255,5292,5306,5312,5316,5321,5329,5389,5426,5470,5471,5488,5489,5524,5766,5772,5788,5899,6197,6285,6297,6298,6394,6430,6433,6434,6436,6437,6443,6450,6451,6452,6456,6457,6488,6495,6814,6815,7490,8372,8471,8686,8846,8890,9321,9423,9444,9568,10050,10253,10256,10257,10258,10259,10260,10288,10384,10441,10887,11531,11614,11620,11633,11681,11771,11772,11870,12203,12227,12228,12283,12358,12502,12507,12524))->get('google_disease')->result_array();
        foreach($diseases as $d){
            $redis->rPush('google_pic_ext',$d['id']);
        }
    }

}